package com.ss.web.tagmark.init;

import java.util.Date;
import java.util.List;

import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import com.ss.web.tagmark.dao.TagDao;
import com.ss.web.tagmark.dao.UserDao;
import com.ss.web.tagmark.entity.TagInfo;
import com.ss.web.tagmark.entity.User;

/**
 * 实体类中页面提交 的内容 进行类型转换的配置
 * 
 * @author Administrator
 *
 */
@Configuration
public class CustomEditorConfiguration {
	@Autowired
	private WebBindingInitializer webBindingInitializer;

	@Autowired
	public void setWebBindingInitializer(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
		requestMappingHandlerAdapter.setWebBindingInitializer(webBindingInitializer);
	}

	/**
	 * 自定义实体转换器
	 *
	 * @author taofucheng
	 *
	 */
	@Configuration
	public static class CustomWebBindingInitializer implements WebBindingInitializer {
		@Autowired
		private UserDao userDao;
		@Autowired
		private TagDao tagDao;
		@Autowired
		private Validator validator;

		@Override
		public void initBinder(WebDataBinder binder) {
			binder.addValidators(new EntityValidator(validator));
			binder.registerCustomEditor(Date.class, new CustomDateEditor());
			binder.registerCustomEditor(List.class, "users", new CollectionEntityEditor<User>(userDao));
			binder.registerCustomEditor(List.class, "tags", new CollectionEntityEditor<TagInfo>(tagDao));
		}

	}

}
